//
//  HZHomeView.m
//  HZTC_Stock
//
//  Created by MayerF on 17/3/9.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZHomeView.h"
#import "HZSelectedStockView.h"
#import "HZHuShenView.h"
#import "HZMarketCommonView.h"
#import "HZBlockView.h"
#import "HZMoreView.h"
#import "MYUpDownBtn.h"


typedef NS_ENUM(NSInteger, kShowView) {
    kShow_huShen_View,
    kShow_block_View,
    kShow_chuangYe_View,
    kShow_gangGuTong_View,
    kShow_more_View,
};

@interface HZHomeView()<UIScrollViewDelegate>
{
    //缓存部分数据
    /*BTex*/
    NSArray *_pBlockIndexCache;//板块okex
    NSArray *_pZhangFuTopCache;//涨幅榜
    NSArray *_pHuanShouTopCache;//换手率榜
    NSArray *_pLiangBiCache;//量比榜
    /*板块*/
    NSArray *_pGaiNianCache;
    NSArray *_pHangYeCache;
    NSArray *_pZoneCache;
    /*创业*/
    NSArray *_pCYZhangFuTopCache;//创业涨幅榜
    NSArray *_pCYHuanShouTopCache;//换手率榜
    NSArray *_pCYLiangBiCache;//量比榜
    /*港股通*/
    NSArray *_pHKHuGangCache;
    NSArray *_pHKShenGangCache;
    NSArray *_pHKHuGuCache;
    NSArray *_pHKShenGuCache;
}
@property (nonatomic, strong)UIScrollView *pScrollView;
@property (nonatomic, strong)HZHuShenView *pHuShenView;
@property (nonatomic, strong)UIView       *pScrollBgView;
@property (nonatomic, strong)HZSelectedStockView *pSelectedView;
@property (nonatomic, strong)HZMarketCommonView  *pCommonView;
@property (nonatomic, strong)HZBlockView  *pBlockView;
@property (nonatomic, strong)HZMoreView   *pMoreView;
@property (nonatomic, assign)kShowView     nShowView;

@property (nonatomic, strong)MYUpDownBtn *pAlertInfo;

@property (nonatomic, strong)NSMutableArray *pIsOpenListArray; //列表打开闭合数组
@end

@implementation HZHomeView
- (void)p_loadAlertInfo
{
    if (!_pAlertInfo) {
        _pAlertInfo = [MYUpDownBtn new];
        [_pAlertInfo setImage:[UIImage imageNamed:@"empty"] forState:UIControlStateNormal];
        [_pAlertInfo setImage:[UIImage imageNamed:@"empty"] forState:UIControlStateHighlighted];
        _pAlertInfo.imageView.contentMode = UIViewContentModeCenter;
        [_pAlertInfo setTitle:@"请登录账户以获取自选股服务" forState:UIControlStateNormal];
        [_pAlertInfo setTitleColor:kMYHexToRGB(0x888888, 1) forState:UIControlStateNormal];
        _pAlertInfo.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_pAlertInfo];
        [_pAlertInfo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self);
            make.width.mas_equalTo(self);
            make.height.mas_equalTo(70);
        }];
    }
}

#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self p_commonInit];
    }
    return self;
}

- (void)p_commonInit
{
    self.pScrollView = [UIScrollView new];
    _pScrollView.delegate = self;
    _pScrollView.bounces = NO;
    _pScrollView.pagingEnabled = YES;
    _pScrollView.scrollEnabled = NO;
    
    self.pScrollBgView = [UIView new];
    _pScrollBgView.backgroundColor = [UIColor clearColor];
    self.pSelectedView = [HZSelectedStockView new];
    _pSelectedView.parent = self;
    _pSelectedView.hidden = YES;
    [self addSubview:_pScrollView];
    [_pScrollView addSubview:_pScrollBgView];
    [_pScrollBgView addSubview:_pSelectedView];
    [self p_initConstraints];
    
    _nShowView = kShow_huShen_View;//默认
    _pBlockIndexCache = [NSArray new];
    _pZhangFuTopCache = [NSArray new];
    _pHuanShouTopCache = [NSArray new];
    _pLiangBiCache = [NSArray new];
    _pGaiNianCache = [NSArray new];
    _pHangYeCache = [NSArray new];
    _pZoneCache = [NSArray new];
    _pCYZhangFuTopCache = [NSArray new];
    _pCYHuanShouTopCache = [NSArray new];
    _pCYLiangBiCache = [NSArray new];
    _pHKHuGangCache = [NSArray new];
    _pHKShenGangCache = [NSArray new];
    _pHKHuGuCache = [NSArray new];
    _pHKShenGuCache = [NSArray new];
    
    [self p_showRelatedView];
    
    _pIsOpenListArray = [NSMutableArray arrayWithObjects:@(YES),@(YES),@(YES),@(YES), nil];
}

#pragma mark -
#pragma mark private
- (void)p_initConstraints
{
    [_pScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
    [_pScrollBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(_pScrollView);
        make.width.mas_equalTo(self).multipliedBy(2.f);
        make.height.mas_equalTo(_pScrollView);
    }];
    [_pSelectedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pScrollBgView);
        make.left.mas_equalTo(_pScrollBgView);
        make.bottom.mas_equalTo(_pScrollBgView);
        make.width.mas_equalTo(_pScrollBgView).multipliedBy(0.5f);
    }];
}

- (void)p_loadCommonView
{
    if (!_pCommonView) {
        _pCommonView = [HZMarketCommonView new];
        _pCommonView.parent = self;
        [_pScrollBgView addSubview:_pCommonView];
        //首次加载缓存数据
        [_pCommonView updateNewData:_pBlockIndexCache];
        [_pCommonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pScrollBgView);
            make.left.mas_equalTo(_pSelectedView.mas_left);
            make.right.mas_equalTo(_pScrollBgView);
            make.width.mas_equalTo(_pSelectedView);
        }];
    }
    _pCommonView.hidden = NO;
}

- (void)p_loadHuShenView
{
    if (!_pHuShenView) {
        _pHuShenView = [HZHuShenView new];
        _pHuShenView.parent = self;
        [_pScrollBgView addSubview:_pHuShenView];
        //初始化加载缓存的BTex数据
        [_pHuShenView updateNewData:[self p_loadContentData:kShow_huShen_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_huShen_View]];
        [_pHuShenView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pCommonView.mas_bottom);
            make.left.mas_equalTo(_pSelectedView.mas_left);
            make.right.mas_equalTo(_pScrollBgView);
            make.bottom.mas_equalTo(_pScrollBgView);
            make.width.mas_equalTo(_pSelectedView);
        }];
    }
    
    _pHuShenView.hidden = NO;
}

- (void)p_loadBlockView
{
    if (!_pBlockView) {
        _pBlockView = [HZBlockView new];
        _pBlockView.parent = self;
        [_pScrollBgView addSubview:_pBlockView];
        //初始化加载缓存的板块数据
        [_pBlockView updateNewData:[self p_loadContentData:kShow_block_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_block_View]];
        [_pBlockView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pCommonView.mas_bottom);
            make.left.mas_equalTo(_pSelectedView.mas_left);
            make.right.mas_equalTo(_pScrollBgView);
            make.bottom.mas_equalTo(_pScrollBgView);
            make.width.mas_equalTo(_pSelectedView);
        }];
    }
    _pBlockView.hidden = NO;
}

- (void)p_loadMoreView
{
    if (!_pMoreView) {
        _pMoreView = [HZMoreView new];
        _pMoreView.parent = self;
        [self addSubview:_pMoreView];
        [_pMoreView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(44);
            make.left.mas_equalTo(_pSelectedView.mas_left);
            make.right.mas_equalTo(_pScrollBgView);
            make.bottom.mas_equalTo(_pScrollBgView);
            make.width.mas_equalTo(_pSelectedView);
        }];
    }
    _pMoreView.hidden = NO;
}

- (void)hiddenView
{
    _pBlockView.hidden = YES;
    _pHuShenView.hidden = YES;
    _pBlockView.hidden = YES;
    _pMoreView.hidden = YES;
}

- (void)p_showRelatedView
{
    [self p_resetIsOpenListArray];
    [self p_loadCommonView];
    [self hiddenView];
    switch (_nShowView) {
        case kShow_huShen_View:
            [self p_loadHuShenView];
            [_pHuShenView updateNewData:[self p_loadContentData:kShow_huShen_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_huShen_View]];
            break;
        case kShow_chuangYe_View:
            [self p_loadHuShenView];
            [_pHuShenView updateNewData:[self p_loadContentData:kShow_chuangYe_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_chuangYe_View]];
            break;
        case kShow_gangGuTong_View:
            [self p_loadHuShenView];
            [_pHuShenView updateNewData:[self p_loadContentData:kShow_gangGuTong_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_gangGuTong_View]];
            break;
        case kShow_block_View:
            [self p_loadBlockView];
            [_pBlockView updateNewData:[self p_loadContentData:kShow_block_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_block_View]];
            break;
        case kShow_more_View:
            [self p_loadMoreView];
            break;
        default:
            break;
    }
}

- (void)p_transferEnumWith:(NSString *)title
{
    if ([title isEqualToString:kHZBanKuai]) {
        _nShowView = kShow_block_View;
    }else if([title isEqualToString:kHZHuShen]) {
        _nShowView = kShow_huShen_View;
    }else if([title isEqualToString:kHZChuangYe]) {
        _nShowView = kShow_chuangYe_View;
    }else if([title isEqualToString:kHZGangGuTong]) {
        _nShowView = kShow_gangGuTong_View;
    }else if([title isEqualToString:kHZGengDuo]) {
        _nShowView = kShow_more_View;
    }
}

- (void)p_isOpenListPackage:(NSMutableArray *)listarray
{
    for (int i = 0; i < listarray.count; i++) {
        BOOL isOpen = [_pIsOpenListArray[i] boolValue];
        if (!isOpen) {
            [listarray replaceObjectAtIndex:i withObject:[NSArray array]];
        }
    }
}
- (void)p_resetIsOpenListArray
{
    for (int i = 0; i < _pIsOpenListArray.count; i++) {
        [_pIsOpenListArray replaceObjectAtIndex:i withObject:@(YES)];
    }
}

- (NSArray *)p_loadContentData:(kShowView)showView
{
    NSMutableArray *pTempArray = [NSMutableArray arrayWithCapacity:5];
    switch (showView) {
        case kShow_huShen_View:
        {
            [pTempArray addObject:_pBlockIndexCache];
            [pTempArray addObject:_pZhangFuTopCache];
            [pTempArray addObject:_pHuanShouTopCache];
            [pTempArray addObject:_pLiangBiCache];
        }
            break;
        case kShow_block_View:
        {
            [pTempArray addObject:_pGaiNianCache];
            [pTempArray addObject:_pHangYeCache];
            [pTempArray addObject:_pZoneCache];
        }
            break;
        case kShow_chuangYe_View:
        {
            [pTempArray addObject:_pCYZhangFuTopCache];
            [pTempArray addObject:_pCYHuanShouTopCache];
            [pTempArray addObject:_pCYLiangBiCache];
        }
            break;
        case kShow_gangGuTong_View:
        {
            [pTempArray addObject:_pHKHuGangCache];
            [pTempArray addObject:_pHKShenGangCache];
            [pTempArray addObject:_pHKHuGuCache];
            [pTempArray addObject:_pHKShenGuCache];
        }
            break;
            
        default:
            break;
    }
    
    [self p_isOpenListPackage:pTempArray];
    
    return pTempArray;
}
- (NSArray *)p_loadHeaderTitleData:(kShowView)showView
{
    NSMutableArray *pTempArray = [NSMutableArray arrayWithCapacity:5];
    switch (showView) {
        case kShow_huShen_View:
        {
            [pTempArray addObject:@"hot"];
            [pTempArray addObject:@"涨幅榜"];
            [pTempArray addObject:@"换手率榜"];
            [pTempArray addObject:@"量比榜"];
        }
            break;
        case kShow_block_View:
        {
            [pTempArray addObject:@"领涨概念"];
            [pTempArray addObject:@"领涨行业"];
            [pTempArray addObject:@"领涨地区"];
        }
            break;
        case kShow_chuangYe_View:
        {
            [pTempArray addObject:@"涨幅榜"];
            [pTempArray addObject:@"换手率榜"];
            [pTempArray addObject:@"量比榜"];
        }
            break;
        case kShow_gangGuTong_View:
        {
            [pTempArray addObject:@"沪港通"];
            [pTempArray addObject:@"深港通"];
            [pTempArray addObject:@"沪股通"];
            [pTempArray addObject:@"深股通"];
        }
            break;
            
        default:
            break;
    }
    
    return pTempArray;
}

- (void)p_sendInfoWithWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter
{
    if (![objc isKindOfClass:[NSNumber class]]) {
        return;
    }
    kHZApiType nApiType = [objc integerValue];
    switch (nApiType) {
        case kHZApi_Own_Type:
        {
            [_pSelectedView downToChildWithObjc:objc withType:type withParamter:paramter];
        }
            break;
        case kHZApi_BlockIndex_Type:
        {
            if ([paramter isKindOfClass:[NSArray class]] && paramter) {
                _pBlockIndexCache = paramter;
                [_pCommonView updateNewData:paramter];
                if (_nShowView == kShow_huShen_View) {
                    [_pHuShenView updateNewData:[self p_loadContentData:kShow_huShen_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_huShen_View]];
                }
            }
        }
            break;
        case kHZApi_ZhangFuTop_Type:
        {
            if ([paramter isKindOfClass:[NSArray class]] && paramter) {
                _pZhangFuTopCache = paramter;
                if (_nShowView == kShow_huShen_View) {
                    [_pHuShenView updateNewData:[self p_loadContentData:kShow_huShen_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_huShen_View]];
                }
            }
        }
            break;
        case kHZApi_HuanShouTop_Type:
        {
            if ([paramter isKindOfClass:[NSArray class]] && paramter) {
                _pHuanShouTopCache = paramter;
                if (_nShowView == kShow_huShen_View) {
                    [_pHuShenView updateNewData:[self p_loadContentData:kShow_huShen_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_huShen_View]];
                }
            }
        }
            break;
        case kHZApi_LiangBiTop_Type:
        {
            if ([paramter isKindOfClass:[NSArray class]] && paramter) {
                _pLiangBiCache = paramter;
                if (_nShowView == kShow_huShen_View) {
                    [_pHuShenView updateNewData:[self p_loadContentData:kShow_huShen_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_huShen_View]];
                }
            }
        }
            break;
        case kHZApi_GaiNian_Type:
        {
            if ([paramter isKindOfClass:[NSArray class]] && paramter) {
                _pGaiNianCache = paramter;
                if (_nShowView == kShow_block_View) {
                    [_pBlockView updateNewData:[self p_loadContentData:kShow_block_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_block_View]];
                }
            }
        }
            break;
        case kHZApi_HangYe_Type:
        {
            if ([paramter isKindOfClass:[NSArray class]] && paramter) {
                _pHangYeCache = paramter;
                if (_nShowView == kShow_block_View) {
                    [_pBlockView updateNewData:[self p_loadContentData:kShow_block_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_block_View]];
                }
            }
        }
            break;
        case kHZApi_Zone_Type:
        {
            if ([paramter isKindOfClass:[NSArray class]] && paramter) {
                _pZoneCache = paramter;
                if (_nShowView == kShow_block_View) {
                    [_pBlockView updateNewData:[self p_loadContentData:kShow_block_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_block_View]];
                }
            }
        }
            break;
        case kHZApi_ZhangFuTop_CY_Type:
        {
            if ([paramter isKindOfClass:[NSArray class]] && paramter) {
                _pCYZhangFuTopCache = paramter;
                if (_nShowView == kShow_chuangYe_View) {
                    [_pHuShenView updateNewData:[self p_loadContentData:kShow_chuangYe_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_chuangYe_View]];
                }
            }
        }
            break;
        case kHZApi_HuanShouTop_CY_Type:
        {
            if ([paramter isKindOfClass:[NSArray class]] && paramter) {
                _pCYHuanShouTopCache = paramter;
                if (_nShowView == kShow_chuangYe_View) {
                    [_pHuShenView updateNewData:[self p_loadContentData:kShow_chuangYe_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_chuangYe_View]];
                }
            }
        }
            break;
        case kHZApi_LiangBiTop_CY_Type:
        {
            if ([paramter isKindOfClass:[NSArray class]] && paramter) {
                _pCYLiangBiCache = paramter;
                if (_nShowView == kShow_chuangYe_View) {
                    [_pHuShenView updateNewData:[self p_loadContentData:kShow_chuangYe_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_chuangYe_View]];
                }
            }
        }
            break;
        case kHZApi_HuGangTong_Type:
        {
            if ([paramter isKindOfClass:[NSArray class]] && paramter) {
                _pHKHuGangCache = paramter;
                if (_nShowView == kShow_gangGuTong_View) {
                    [_pHuShenView updateNewData:[self p_loadContentData:kShow_gangGuTong_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_gangGuTong_View]];
                }
            }
        }
            break;
        case kHZApi_ShenGangTong_Type:
        {
            if ([paramter isKindOfClass:[NSArray class]] && paramter) {
                _pHKShenGangCache = paramter;
                if (_nShowView == kShow_gangGuTong_View) {
                    [_pHuShenView updateNewData:[self p_loadContentData:kShow_gangGuTong_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_gangGuTong_View]];
                }
            }
        }
            break;
        case kHZApi_HuGuTong_Type:
        {
            if ([paramter isKindOfClass:[NSArray class]] && paramter) {
                _pHKHuGuCache = paramter;
                if (_nShowView == kShow_gangGuTong_View) {
                    [_pHuShenView updateNewData:[self p_loadContentData:kShow_gangGuTong_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_gangGuTong_View]];
                }
            }
        }
            break;
        case kHZApi_ShenGuTong_Type:
        {
            if ([paramter isKindOfClass:[NSArray class]] && paramter) {
                _pHKShenGuCache = paramter;
                if (_nShowView == kShow_gangGuTong_View) {
                    [_pHuShenView updateNewData:[self p_loadContentData:kShow_gangGuTong_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_gangGuTong_View]];
                }
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark -
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    CGPoint point = scrollView.contentOffset;
//    if (point.x > 0) {
//        [self p_showRelatedView];
//    }
    [self.parent upToParentWithObjc:self withType:kHZ_setsegmentControlState_Action withParamter:scrollView];
}

#pragma mark -
- (void)upToParentWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
    switch (type) {
        case kHZ_jumpSingleStock_Action:
        case kHZ_BlockListDetailClick_Action:
        case KMSClickLoginAndDownLoadOwnStockType:
        {
            [self.parent upToParentWithObjc:objc withType:type withParamter:paramter];
        }
            break;
        case kHZ_stockListMore_Action:
        {
            NSString *titleTemp = (NSString *)paramter;
            if([titleTemp isEqualToString:@"涨幅榜"] || [titleTemp isEqualToString:@"换手率榜"]
               || [titleTemp isEqualToString:@"量比榜"]){
                if(_nShowView == kShow_huShen_View){
                    NSString *title = [NSString stringWithFormat:@"BTex--%@",titleTemp];
                    [self.parent upToParentWithObjc:objc withType:type withParamter:title];
                    return;
                }else{
                    NSString *title = [NSString stringWithFormat:@"创业板--%@",titleTemp];
                    [self.parent upToParentWithObjc:objc withType:type withParamter:title];
                    return;
                }
            }else if ([titleTemp isEqualToString:@"领涨概念"] || [titleTemp isEqualToString:@"领涨行业"]
                      || [titleTemp isEqualToString:@"领涨地区"]) {
                [self.parent upToParentWithObjc:objc withType:type withParamter:titleTemp];
                return;
            }
            

            [self.parent upToParentWithObjc:objc withType:type withParamter:paramter];
        }
            break;
        case kHZ_radioGroupClick_Action:
        {
            if ([paramter isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)paramter;
                [self p_transferEnumWith:btn.currentTitle];
                [self p_showRelatedView];
                [_pHuShenView resetTableViewOffset];
            }
        }
            break;
        case kHZ_ClickMarketSectionHeader_Action:
        {
            NSInteger nIndex = [paramter integerValue];
            BOOL isOpen = [_pIsOpenListArray[nIndex] boolValue];
            [_pIsOpenListArray replaceObjectAtIndex:nIndex withObject:@(!isOpen)];
            [_pHuShenView updateNewData:[self p_loadContentData:_nShowView] andHeaderTitle:[self p_loadHeaderTitleData:_nShowView]];
            [_pBlockView updateNewData:[self p_loadContentData:kShow_block_View] andHeaderTitle:[self p_loadHeaderTitleData:kShow_block_View]];
        }
            break;
            
        default:
            break;
    }
}
- (void)downToChildWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
    switch (type) {
        case kHZ_segmentControlClick_Action:
        {
            UISegmentedControl *segment = (UISegmentedControl *)paramter;
            if (segment.selectedSegmentIndex == 0) {
                [_pScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
            }else {
                [_pScrollView setContentOffset:CGPointMake(CGRectGetWidth(self.frame), 0) animated:YES];
            }
        }
            break;
        case kHZ_updateNewData_Action:
        {
            [self p_sendInfoWithWithObjc:objc withType:type withParamter:paramter];
        }
            break;
        case KMSClickLoginAndDownLoadOwnStockType:
        {
            [_pSelectedView updateAlertInfo];
        }
            break;
            
        default:
            break;
    }
}

- (void)updateThemeColor
{
    [_pSelectedView updateThemeColor];
}

@end
